<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="docker">
      docker使用记录
     </h1>
     <hr/>
     <h2 id="_1">
      使用小贴士
     </h2>
     <ul>
      <li>
       在没有进行配置之前，使用docker必须赋予root权限
      </li>
     </ul>
     <h2 id="docker_1">
      docker 的安装
     </h2>
     <ul>
      <li>
       <a href="https://yq.aliyun.com/articles/110806?spm=a2c4e.11153940.blogcont29941.13.520269d6XtzWqG">
        Docker CE 镜像源站
       </a>
      </li>
      <li>
       <a href="http://blog.csdn.net/qq_27818541/article/details/73647797">
        安装教程
       </a>
      </li>
      <li>
       <a href="https://cr.console.aliyun.com/?accounttraceid=3480ea01-1b31-4cb1-a169-6c220739cae6#/accelerator">
        镜像加速地址
       </a>
      </li>
      <li>
       安装过程中的依赖问题解决：sudo apt-get install -f -y
      </li>
      <li>
       <a href="https://linuxize.com/post/how-to-install-and-use-docker-on-centos-7/">
        How To Install and Use Docker on CentOS 7
       </a>
      </li>
     </ul>
     <h3 id="centos7">
      centos7
     </h3>
     <p>
      如果出现冲突，卸载相应冲突的软件即可
     </p>
     <div class="codehilite">
      <pre><span></span><code>yum update
yum install yum-utils device-mapper-persistent-data lvm2 -y
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce -y
systemctl start docker
systemctl <span class="nb">enable</span> docker
</code></pre>
     </div>
     <h3 id="centos8">
      centos8
     </h3>
     <div class="codehilite">
      <pre><span></span><code>yum install -y yum-utils  device-mapper-persistent-data  lvm2
yum-config-manager  --add-repo   https://download.docker.com/linux/centos/docker-ce.repo
<span class="c1"># https://download.docker.com/linux/centos/7/x86_64/stable/Packages/,containerd.io-1.2.6-3.3.el7.x86_64.rpm  </span>
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
yum localinstall -y containerd.io-1.2.6-3.3.el7.x86_64.rpm
<span class="c1"># problem with installed package podman-2.0.5-5.module_el8.3.0+512+b3b58dca.x86_64</span>
<span class="c1"># yum erase podman buildah</span>
yum install docker-ce docker-ce-cli

systemctl start docker
</code></pre>
     </div>
     <h2 id="_2">
      与宿主机交互类
     </h2>
     <ul>
      <li>
       单个文件挂载：将宿主的文件进行挂载时，有时docker里面对文件操作会改变宿主机，而宿主机里面操作不会改变，这个好像涉及到一些问题，不建议这样进行挂载，可以进行目录挂载
      </li>
      <li>
       目录挂载：目录挂载可以完美实现文件共享功能，docker和宿主机的操作都会改变文件（Windows里c:/xx/xx/即可）
      </li>
     </ul>
     <h3 id="windows">
      Windows网络互通
     </h3>
     <div class="codehilite">
      <pre><span></span><code>ipconfig

以太网适配器 vEthernet <span class="o">(</span>Default Switch<span class="o">)</span>:

   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::3004:9659:3139:af85%21
   IPv4 地址 . . . . . . . . . . . . : <span class="m">172</span>.17.220.241
   子网掩码  . . . . . . . . . . . . : <span class="m">255</span>.255.255.240
   默认网关. . . . . . . . . . . . . :

route add -p <span class="m">172</span>.17.0.0 mask <span class="m">255</span>.255.255.240 <span class="m">192</span>.168.250.65

route add -p <span class="m">172</span>.17.0.0 mask <span class="m">255</span>.255.255.240 <span class="m">10</span>.0.75.2

cat C:<span class="se">\W</span>indows<span class="se">\S</span>ystem32<span class="se">\d</span>rivers<span class="se">\e</span>tc<span class="se">\h</span>osts
<span class="c1"># Added by Docker Desktop</span>
<span class="m">10</span>.33.0.119 host.docker.internal
<span class="m">10</span>.33.0.119 gateway.docker.internal
<span class="c1"># To allow the same kube context to work on the host and the container:</span>
<span class="m">127</span>.0.0.1 kubernetes.docker.internal
</code></pre>
     </div>
     <h2 id="_3">
      使用中需要注意的问题
     </h2>
     <ul>
      <li>
       <a href="https://yq.aliyun.com/articles/272173">
        空间的清理
       </a>
      </li>
     </ul>
     <h2 id="ssh">
      ssh安装
     </h2>
     <div class="codehilite">
      <pre><span></span><code>yum install -y openssh-server
/usr/sbin/sshd -D
</code></pre>
     </div>
     <h2 id="_4">
      常规操作
     </h2>
     <h3 id="_5">
      进入正在运行中的容器
     </h3>
     <ul>
      <li>
       docker exec -ti dockernameorid /bin/bash
      </li>
     </ul>
     <h3 id="_6">
      导出镜像到本地
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="o">#</span> <span class="n">linux</span>
<span class="n">docker</span> <span class="n">save</span> <span class="n">co</span> <span class="o">&gt;</span> <span class="n">file</span>

<span class="o">#</span> <span class="n">windows</span>
<span class="n">docker</span> <span class="n">save</span> <span class="c1">--output test.tar test:test</span>
</code></pre>
     </div>
     <h3 id="tardocker">
      导入tar包到本地docker镜像
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="o">#</span> <span class="n">linux</span>
<span class="n">docker</span> <span class="k">load</span> <span class="o">&lt;</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="k">new</span><span class="o">-</span><span class="n">image</span><span class="p">.</span><span class="n">tar</span>

<span class="o">#</span> <span class="n">windows</span> 
<span class="n">docker</span> <span class="k">load</span> <span class="c1">--input .\test.tar</span>
</code></pre>
     </div>
     <h3 id="ip">
      查看ip地址
     </h3>
     <p>
      docker inspect --format='{{.NetworkSettings.IPAddress}}' mongo
     </p>
     <h3 id="_7">
      映射连接问题
     </h3>
     <p>
      docker toolbox/desktop 使用上有些区别，特别是 toolbox 需要用启动时的 IP
地址不对,可能不是onlyhost模式,使用下面的地址即可
docker-machine ip default
     </p>
     <h3 id="docker_2">
      Docker容器向宿主机传送文件
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="err">格式</span><span class="o">:</span>
<span class="n">docker</span> <span class="n">cp</span> <span class="n">container_id</span><span class="o">:&lt;</span><span class="n">docker容器内的路径</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="err">本地保存文件的路径</span><span class="o">&gt;</span>
<span class="err">比如</span><span class="o">:</span>
<span class="n">docker</span> <span class="n">cp</span> <span class="mi">10704</span><span class="n">c9eb7bb</span><span class="o">:/</span><span class="n">root</span><span class="sr">/test.text /home/vagrant/</span><span class="n">test</span><span class="o">.</span><span class="na">txt</span>
</code></pre>
     </div>
     <h3 id="docker_3">
      宿主机向Docker容器传送文件
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="err">格式</span><span class="o">:</span>
<span class="n">docker</span> <span class="n">cp</span> <span class="err">本地文件的路径</span> <span class="n">container_id</span><span class="o">:&lt;</span><span class="n">docker容器内的路径</span><span class="o">&gt;</span>

<span class="err">比如</span><span class="o">:</span>
<span class="n">docker</span> <span class="n">cp</span>  <span class="sr">/home/vagrant/test.txt 10704c9eb7bb:/root/</span><span class="n">test</span><span class="o">.</span><span class="na">text</span>
</code></pre>
     </div>
     <h3 id="_8">
      添加镜像加速器
     </h3>
     <h4 id="centos">
      centos
     </h4>
     <div class="codehilite">
      <pre><span></span><code>vim /etc/docker/daemon.json

<span class="o">{</span>
  <span class="s2">"registry-mirrors"</span>: <span class="o">[</span><span class="s2">"https://vmnyyjzt.mirror.aliyuncs.com"</span><span class="o">]</span>
<span class="o">}</span>

systemctl daemon-reload
systemctl restart docker
</code></pre>
     </div>
     <h4 id="windows-docker-toolbox">
      windows docker toolbox
     </h4>
     <div class="codehilite">
      <pre><span></span><code>docker-machine rm default
docker-machine create --engine-registry-mirror<span class="o">=</span>https://vmnyyjzt.mirror.aliyuncs.com -d virtualbox default
docker-machine env default
<span class="nb">eval</span> <span class="s2">"</span><span class="k">$(</span>docker-machine env default<span class="k">)</span><span class="s2">"</span>
docker info

<span class="c1"># docker安装需要最新的boot2docker.iso，从docker的安装界面复制网址，我安装的日期是20180823，此时的boot2docker.iso地址为boot2docker.iso，这个地址貌似被墙了，需要翻墙下载，将下载好的文件放到对应文件夹下，我的电脑上为C:\Users\lzy\.docker\machine\cache</span>
</code></pre>
     </div>
     <h4 id="_9">
      自动启动
     </h4>
     <div class="codehilite">
      <pre><span></span><code><span class="o">#</span> <span class="err">–</span><span class="n">restart参数有三个可选值</span><span class="err">：</span><span class="k">no</span><span class="p">,</span><span class="k">on</span><span class="o">-</span><span class="n">failure</span><span class="p">,</span><span class="n">always</span>

<span class="n">docker</span> <span class="n">run</span> <span class="o">******</span> <span class="c1">--restart=always</span>

<span class="n">docker</span> <span class="k">update</span> <span class="c1">--restart=on-failure:3 [容器名]</span>
</code></pre>
     </div>
     <h4 id="_10">
      公用宿主机网络
     </h4>
     <div class="codehilite">
      <pre><span></span><code><span class="k">docker</span> <span class="s">run</span> <span class="s">-tid</span> <span class="s">--name</span> <span class="s">centos8</span> <span class="s">--network</span> <span class="s">host</span> <span class="n">centos</span><span class="p">:</span><span class="mi">8</span>
</code></pre>
     </div>
     <h2 id="_11">
      一些问题
     </h2>
     <h3 id="win10-docker-wsl">
      <a href="https://stackoverflow.com/questions/62441307/how-can-i-change-the-location-of-docker-images-when-using-wsl2-with-windows-10-h">
       win10 docker wsl 镜像存储位置设置
      </a>
     </h3>
     <div class="codehilite">
      <pre><span></span><code>First, shut down your docker desktop by right click on the Docker Desktop icon and <span class="k">select</span> Quit Docker Desktop

Then, open your <span class="nb">command</span> prompt:

wsl --list -v
You should be able to see, make sure the STATE <span class="k">for</span> both is Stopped.

  NAME                   STATE           VERSION
* docker-desktop         Stopped         <span class="m">2</span>
  docker-desktop-data    Stopped         <span class="m">2</span>
Export docker-desktop-data into a file

wsl --export docker-desktop-data <span class="s2">"D:\Docker\wsl\data\docker-desktop-data.tar"</span>
Unregister docker-desktop-data from wsl, note that after this, your ext4.vhdx file would automatically be removed

wsl --unregister docker-desktop-data
Import the docker-desktop-data back to wsl, but now the ext4.vhdx would reside in different drive/directory:

wsl --import docker-desktop-data <span class="s2">"D:\Docker\wsl\data"</span> <span class="s2">"D:\Docker\wsl\data\docker-desktop-data.tar"</span> --version <span class="m">2</span>
Start the Docker Desktop again and it should work

You may delete the <span class="s2">"D:\Docker\wsl\data\docker-desktop-data.tar"</span> file <span class="o">(</span>NOT the ext4.vhdx file<span class="o">)</span> <span class="k">if</span> everything looks good <span class="k">for</span> you after verifying
</code></pre>
     </div>
     <h3 id="_12">
      重新启动镜像
     </h3>
     <p>
      docker system prune -a
     </p>
     <h3 id="none">
      清理为None的镜像
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="o">#</span> <span class="n">linux</span>
<span class="n">docker</span> <span class="n">rmi</span> <span class="o">-</span><span class="n">f</span> <span class="err">$</span><span class="p">(</span><span class="n">docker</span> <span class="n">images</span> <span class="o">|</span> <span class="n">grep</span> <span class="ss">"none"</span> <span class="o">|</span> <span class="n">awk</span> <span class="s1">'{print $3}'</span><span class="p">)</span>

<span class="o">#</span> <span class="n">linux</span>
</code></pre>
     </div>
     <h3 id="_13">
      清理已经停止的容器
     </h3>
     <div class="codehilite">
      <pre><span></span><code>docker container prune
</code></pre>
     </div>
     <ul>
      <li>
       <a href="https://www.jianshu.com/p/6978cc343150">
        删除Docker没用的镜像，容器，网络，卷，为你的主机“瘦身”
       </a>
      </li>
     </ul>
     <h3 id="dockertagtag">
      docker重新打tag并删除原tag的镜像
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="err">docker tag repository:tag repository:newTag</span>
</code></pre>
     </div>
     <h3 id="2375">
      开发2375端口给外网
     </h3>
     <h4 id="windows_1">
      Windows
     </h4>
     <p>
      Dockers Engine加上下面的配置：
     </p>
     <div class="codehilite">
      <pre><span></span><code>"hosts": ["tcp://0.0.0.0:2375"]
</code></pre>
     </div>
     <p>
      使用命令进行端口代理：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="n">netsh</span> <span class="n">interface</span> <span class="n">portproxy</span> <span class="n">add</span> <span class="n">v4tov4</span> <span class="n">listenport</span><span class="p">=</span><span class="n">2375</span> <span class="n">connectaddress</span><span class="p">=</span><span class="n">127</span><span class="p">.</span><span class="n">0</span><span class="p">.</span><span class="n">0</span><span class="p">.</span><span class="n">1</span> <span class="n">connectport</span><span class="p">=</span><span class="n">2375</span> <span class="n">listenaddress</span><span class="p">=</span><span class="n">192</span><span class="p">.</span><span class="n">168</span><span class="p">.</span><span class="n">110</span><span class="p">.</span><span class="n">242</span> <span class="n">protocol</span><span class="p">=</span><span class="n">tcp</span>
</code></pre>
     </div>
     <ul>
      <li>
       <a href="https://www.jianshu.com/p/c45d47bd5769?spm=a2c6h.13066369.0.0.69459afav3i1E7">
        Windows开启Docker远程连接
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/qq_16171815/article/details/115701262">
        windows下，解决docker2375端口连不上，telnet不通
       </a>
      </li>
     </ul>
     <h2 id="_14">
      常用仓库链接
     </h2>
     <ul>
      <li>
       <a href="https://hub.docker.com/_/mysql">
        mysql
       </a>
      </li>
     </ul>
     <h2 id="_15">
      参考链接
     </h2>
     <ul>
      <li>
       <a href="http://www.markjour.com/article/docker-no-root.html">
        不加 sudo 执行 Docker 命令
       </a>
      </li>
      <li>
       <a href="https://dev.aliyun.com/search.html">
        阿里云docker仓库
       </a>
      </li>
      <li>
       <a href="https://my.oschina.net/editorial-story/blog/1558880">
        Docker 这九个不同的应用场景
       </a>
      </li>
      <li>
       <a href="https://zhuanlan.zhihu.com/p/38552260">
        Docker最佳实践：构建最小镜像
       </a>
      </li>
      <li>
       <a href="http://dockone.io/article/1035">
        微容器：更小的，更轻便的Docker容器
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/gx947791229/article/details/76512660">
        docker for windows 设置aliyun加速以及改变pull镜像位置
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/qq_40133908/article/details/106388175">
        docker容器中没有ifconfig或ping命令
       </a>
      </li>
     </ul>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
